package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.InspectionWorkLog;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther: bin.liu
 * @Date: 2019/1/11 15:12
 * @Description: 巡检工单日志dao
 */
@Repository("inspectionWorkLogDao")
public class InspectionWorkLogDao implements CrudDao<InspectionWorkLog> {
    private final static Logger logger = LoggerFactory.getLogger(InspectionWorkLogDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("iwl.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String code = queryHash.get("code");
        if (StringUtils.isNotBlank(code)) {
            conditionHash.put("iwl.code = ?{paramIndex}", code);
        }

        String inspectionOrderId = queryHash.get("inspectionOrderId");
        if (StringUtils.isNotBlank(inspectionOrderId)) {
            conditionHash.put("iwl.inspectionOrder.id = ?{paramIndex}", StringConverters.null2Long(inspectionOrderId));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(iwl) from InspectionWorkLog iwl ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<InspectionWorkLog> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select iwl from InspectionWorkLog iwl ", "order by iwl.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<InspectionWorkLog> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, InspectionWorkLog.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public InspectionWorkLog findModel(Long id) {
        return entityManager.find(InspectionWorkLog.class, id);
    }

    @Override
    public Integer add(InspectionWorkLog model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("InspectionWorkLogDaoImpl添加inspectionWorkLog成功！");
        return 1;
    }

    @Override
    public Integer update(InspectionWorkLog model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        InspectionWorkLog inspectionWorkLog = entityManager.find(InspectionWorkLog.class, id);
        entityManager.remove(inspectionWorkLog);
        return 1;
    }

    @Override
    public Integer deleteLogic(InspectionWorkLog model) {
        model.setDeleteStatus(true);
        return 1;
    }

    /**
     * 获取工作日志内部编号
     *
     * @param enterpriseId
     * @return
     */
    public String getCode(String enterpriseId) {
        String code = enterpriseId + "-" + DateTimeUtils.formatDateToStringWithOnlyDate(new Date(), "yyyyMMdd") + "-" + (int) ((Math.random() * 9 + 1) * 100000);
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("code", code);
        List<InspectionWorkLog> inspectionWorkLogs = findList(queryParam);
        if (CollectionUtils.isNotEmpty(inspectionWorkLogs)) {
            return getCode(enterpriseId);
        } else {
            return code;
        }
    }
}
